﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using MyAlbum.Services;

namespace PersonalAlbum
{
    public static class ConfigItemExtenstions
    {
        /// <summary>
        /// Returns the item in HTML
        /// </summary>
        /// <returns></returns>
        public static string ToHtml(this ConfigItem config)
        {
            string html;
            switch (config.ValidationType)
            {
                case ValidationType.boolean:
                    html = "<input type=\"checkbox\" value=\"true\" name=\"" + HttpUtility.HtmlEncode(config.Name) + "\" " + (string.Compare(config.Value, "true", true) == 0 ? "checked=\"checked\"" : null) + " />";
                    break;
                case ValidationType.integer:
                    html = "<input type=\"text\" name=\"" + HttpUtility.HtmlEncode(config.Name) + "\"  value=\"" + HttpUtility.HtmlEncode(config.Value) + "\" class=\"" + (config.Required ? "required " : null) + "digits\" />";
                    break;
                case ValidationType.numeric:
                    html = "<input type=\"text\" name=\"" + HttpUtility.HtmlEncode(config.Name) + "\"  value=\"" + HttpUtility.HtmlEncode(config.Value) + "\" class=\"" + (config.Required ? "required " : null) + "number\" />";
                    break;
                case ValidationType.selection:
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<select id=\"");
                    sb.Append(HttpUtility.HtmlEncode(config.Name));
                    sb.Append("\" name=\"");
                    sb.Append(HttpUtility.HtmlEncode(config.Name));
                    sb.Append("\">");
                    for (int i = 0; i < config.ValueList.Count; i++)
                    {
                        sb.Append("<option");
                        if (config.Value == config.ValueList[i])
                        {
                            sb.Append(" selected=\"selected\"");
                        }
                        sb.Append(">");
                        sb.Append(HttpUtility.HtmlEncode(config.ValueList[i]));
                        sb.Append("</option>");
                    }
                    sb.Append("</select>");
                    html = sb.ToString();
                    break;
                case ValidationType.text:
                    html = "<input type=\"text\" name=\"" + HttpUtility.HtmlEncode(config.Name) + "\" value=\"" + HttpUtility.HtmlEncode(config.Value) + "\" " + (config.Required ? "class=\"text required\"" : null) + " />";
                    break;

                default:
                    throw new ArgumentException(config.ValidationType + " validation type is not recognized", "ValidationType");
            }
            return html;
        }

    }
    

}
